package com.ming.test.page;

public class Encapsulation {
    private String name;//姓名
    private int age;//年龄
    private char sex;//性别

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
//        控制成员变量的值
        this.age = age<150?age:18;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public char getSex() {
        return sex;
    }

    @Override
    public String toString() {
        return "Encapsulation{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }


//   main方法
    public static void main(String[] args){
        Encapsulation encapsulation = new Encapsulation();
//        encapsulation.name = '小明'; //报错，不允许访问
        encapsulation.setName("小明");//正确
        encapsulation.setAge(100);
        encapsulation.setSex('男');
        String string = encapsulation.toString();
        System.out.println(string);
    }
}